package cn.kgc.vue.service.impl;

import cn.kgc.vue.commons.ResponseResult;
import cn.kgc.vue.entity.RolePers;
import cn.kgc.vue.mapper.RolePersMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.vue.entity.Permission;
import cn.kgc.vue.service.PermissionService;
import cn.kgc.vue.mapper.PermissionMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 11839
* @description 针对表【sys_permission】的数据库操作Service实现
* @createDate 2024-06-28 15:23:44
*/
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission>
    implements PermissionService{

    @Resource
    private RolePersMapper rolePersMapper;



    @Transactional
    @Override
    public ResponseResult deletePermission(Integer id) {
        // 1 .判定是否含有子权限
        LambdaQueryWrapper<Permission> lambda = new QueryWrapper<Permission>().lambda();
        lambda.eq(Permission::getPid,id);

        List<Permission> permissions = baseMapper.selectList(lambda);
        if (ObjectUtils.isNotNull(permissions)){
            return  ResponseResult.fail().message("含有子权限,不能删除");
        }
        // 2.删除权限信息
        baseMapper.deleteById(id);

        // 3.删除权限和角色的关联数据
        LambdaQueryWrapper<RolePers> rolePersLambda = new QueryWrapper<RolePers>().lambda();
        rolePersLambda.eq(RolePers::getPerId,id);
        rolePersMapper.delete(rolePersLambda);
        return ResponseResult.success().message("权限删除成功");


    }
}




